#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
const int max_size = 5 + 100;
const int maxn = 15;

int dx[] = {0, -1, 0, 1, 0};
int dy[] = {0, 0, 1, 0, -1};

int a[maxn][maxn], tar[maxn][maxn], tx[maxn], ty[maxn];

int cnt, hx;
bool inside(int x, int y) { return x > 0 && x <= 3 && y > 0 && y <= 3; }
int dis_cal(int x, int y) { return abs(x - tx[a[x][y]]) + abs(y - ty[a[x][y]]); }
// int hx() {
//     int cnt = 0;
//     rf(i, 3) rf(j, 3) {
//         if (a[i][j] == 0) continue;
//         cnt += dis_cal(i, j);
//     }
//     return cnt;
//     // return 8 - cnt;
// }
int maxd;
void dfs(int dep, int x, int y, int last_dir) {
    if (cnt == 8) throw maxd; // arrive
    if (dep > maxd) return;
    if (dep - 1 + hx > maxd) return;
    for (int i = 1; i <= 4; ++i) {
        int nx = x + dx[i], ny = y + dy[i];
        if (!inside(nx, ny)) continue;
        if ((i - 1 + 2) % 4 + 1 == last_dir) continue;
        cnt -= (a[x][y] == tar[x][y]) + (a[nx][ny] == tar[nx][ny]);
        hx -= dis_cal(x, y) + dis_cal(nx, ny);
        swap(a[nx][ny], a[x][y]);
        cnt += (a[x][y] == tar[x][y]) + (a[nx][ny] == tar[nx][ny]);
        hx += dis_cal(x, y) + dis_cal(nx, ny);
        dfs(dep + 1, nx, ny, i);
        cnt -= (a[x][y] == tar[x][y]) + (a[nx][ny] == tar[nx][ny]);
        hx -= dis_cal(x, y) + dis_cal(nx, ny);
        swap(a[nx][ny], a[x][y]);
        cnt += (a[x][y] == tar[x][y]) + (a[nx][ny] == tar[nx][ny]);
        hx += dis_cal(x, y) + dis_cal(nx, ny);
    }
}

int main() {
    int sx, sy;
    rf(i, 3) rf(j, 3) {
        scanf("%1d", &a[i][j]);
        if (a[i][j] == 0) sx = i, sy = j;
    }
    rf(i, 3) rf(j, 3) {
        scanf("%1d", &tar[i][j]);
        tx[tar[i][j]] = i, ty[tar[i][j]] = j;
        if (tar[i][j] == 0) tar[i][j] = -1;
        cnt += (a[i][j] == tar[i][j]);
    }
    rf(i, 3) rf(j, 3) if (a[i][j] == 0) tx[0] = i, ty[0] = j;
    rf(i, 3) rf(j, 3) hx += dis_cal(i, j);
    // cout << hx;
    try {
        maxd = hx - 1;
        while (++maxd) dfs(1, sx, sy, -1);
    } catch (int ans) {
        printf("%d", ans);
    }
}
